/**
 * Created by gaoyj on 2018/6/28.
 */
window.onload = function (ev) {
    topbar.hide();
};

$('#dataTable').bootstrapTable({
    url: '/UserPmController/queryUserPm',
    height: $(window).height() - 200,
    pageSize: 30,
    pageList: [30, 50, 100],
    clickToSelect: true,
    queryParams: queryParams,
    uniqueId: 'id',
    queryParamsType: '',
    sidePagination: 'server',
    responseHandler: function (res) {
        return {
            "total": res.total,//总页数
            "rows": res.resultList   //数据
        };
    },
    columns: [
        {
            checkbox: true
        },
        {
            align: 'center',
            formatter: function (v, row, index) {
                return index + 1
            }
        }, {
            align: 'center',
            field: 'name',
            title: '管理员'
        }, {
            align: 'center',
            field: 'qq',
            title: '联系QQ '
        }, {
            align: 'center',
            field: 'tel',
            title: '联系电话'
        }
    ]
});

function addUserPmFun(v) {
    $("#addUserPmLabel").attr("msg", v)
    if (v == 0) {
        $("#addUserPmLabel").text("新增管理员");
        $("#addUserPmForm input").val("");
    } else {
        $("#addUserPmLabel").text("修改管理员");
        var rows = $('#dataTable').bootstrapTable("getSelections");
        if (rows == null || rows.length === 0) {
            layer.msg("请选择要操作的数据");
            return;
        }
        var row = rows[0];
        $("#name").val(row.name);
        $("#hidId").val(row.id);
        $("#qq").val(row.qq);
        $("#tel").val(row.tel);
    }
    $("#addUserPmModal").modal({backdrop: 'static', keyboard: false});
}

function deleteUserPmFun() {
    var rows = $('#dataTable').bootstrapTable("getSelections");
    if (rows == null || rows.length === 0) {
        layer.msg("请选择要操作的数据");
        return;
    }
    $.ajax({
        type: "post",
        url: "/UserPmController/deleteUserPm",
        data: {id: rows[0].id},
        dataType: "json",
        async: false,
        cache: false,
        success: function (data) {
            if (data.success) {
                $("#dataTable").bootstrapTable("refresh");
                $("#addUserPmModal").modal("hide");
                layer.msg('操作成功');
            } else {
                layer.msg(data.message)
            }
        }
    });
}

$("#addUserPmSubmit").click(function () {
    var msg = $("#addUserPmLabel").attr("msg");
    $("#hidStatus").val(msg)
    $.ajax({
        type: "post",
        url: "/UserPmController/modifyUserPm",
        data: $("#addUserPmForm").serialize(),
        dataType: "json",
        async: false,
        cache: false,
        success: function (data) {
            if (data.success) {
                $("#dataTable").bootstrapTable("refresh");
                $("#addUserPmModal").modal("hide");
                layer.msg('操作成功');
            } else {
                layer.msg(data.message)
            }
        }
    });
})
$("#search").click(function () {
    $("#dataTable").bootstrapTable("refresh");
});

function queryParams(params) {
    return {
        size: params.pageSize,
        page: params.pageNumber,
        adminName: $.trim($("#adminName").val()),
    };
}